<template>
	<view>
		<image src="../../static/logo-2.png" mode="widthFix" class="logo"></image>
		<view class="register-container">
			<input placeholder="输入你的邀请码" class="register-code" maxlength="6" v-model="registerCode" />
			<view class="register-desc">管理员创建员工证账号之后，你可以从你的个人邮箱中获得注册邀请码</view>
			<button class="register-btn" open-type="getUserInfo" @tap="register()">执行注册</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			registerCode: null
		};
	},
	methods: {
		register: function() {
			/* 3-13、前端验证 */
			let that = this;
			if (that.registerCode == null || that.registerCode.length == 0) {
				uni.showToast({
					title: '邀请码不能为空',
					icon: 'none'
				});
				return;
			} else if (/^[0-9]{6}$/.test(that.registerCode) == false) {
				uni.showToast({
					title: '邀请码必须是6为数字',
					icon: 'none'
				});
				return;
			}

			uni.login({
				provider: 'weixin',
				success: function(resp) {
					let code = resp.code;
					/* 用户的唯一标识：openId */
					console.log('微信临时授权字符串===>' + code);
					// 获取用户信息（微信昵称以及微信头像）
					uni.getUserProfile({
						desc: '获取用户信息',
						success: function(resp) {
							let nickName = resp.userInfo.nickName;
							let avatarUrl = resp.userInfo.avatarUrl;
							console.log('nickName==>' + nickName);
							console.log('avatarUrl==>' + avatarUrl);

							/* 3-13、封装Ajax */
							let data = {
								code: code,
								nickname: nickName,
								photo: avatarUrl,
								registerCode: that.registerCode
							};
							that.ajax(that.url.register, 'POST', data, function(resp) {
								let permission = resp.data.permission;
						
								uni.setStorageSync('permission', permission);
								//跳转到index页面
								uni.switchTab({
									url: '../index/index'
								});
							});
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="less">
@import url('register.less');
</style>
